﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Djs.Tools.FileCommander.FileSystem
{
    public class FileGroup
    {
        public FileGroup()
        {
            this.Location = null;
            this.ItemList = new List<FileItem>();
        }
        public string Location { get; private set; }
        public List<FileItem> ItemList { get; private set; }

        public void Load(string location)
        {
            this.Location = location;
            this.ItemList.Clear();

            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(location);
            System.IO.FileSystemInfo[] items = dirInfo.GetFileSystemInfos();

            List<FileItem> fileItems = new List<FileItem>();
            fileItems.Add(new FileItem(this) { FileType = FileType.UpDir });

            foreach (System.IO.FileSystemInfo item in items)
                fileItems.Add(this._CreateNewItem(item));

            fileItems.Sort(FileItem.CompareByName);

            this.ItemList = fileItems;
        }

        protected FileItem _CreateNewItem(System.IO.FileSystemInfo item)
        {
            FileItem fileItem = new FileItem(this);

            if (item is System.IO.DirectoryInfo)
            {
                System.IO.DirectoryInfo dir = item as System.IO.DirectoryInfo;
                fileItem.FileType = FileType.DownDir;
                fileItem.FullName = dir.FullName;
                fileItem.Name = dir.Name;
                fileItem.Extension = "";
                fileItem.Length = 0L;
                fileItem.DateModify = dir.LastWriteTime;
                fileItem.ToolTip = null;
                fileItem.IconKey = null;
            }
            else if (item is System.IO.FileInfo)
            {
                System.IO.FileInfo file = item as System.IO.FileInfo;
                fileItem.FileType = FileType.File;
                fileItem.FullName = file.FullName;
                fileItem.Name = System.IO.Path.GetFileNameWithoutExtension(file.Name);
                string ext = System.IO.Path.GetExtension(file.Name);
                fileItem.Extension = (ext.Length > 1 && ext.StartsWith(".") ? ext.Substring(1) : ext);
                fileItem.Length = file.Length;
                fileItem.DateModify = file.LastWriteTime;
                fileItem.ToolTip = null;
                fileItem.IconKey = null;
            }

            return fileItem;
        }
    }
    public class FileItem
    {
        public FileItem(FileGroup owner)
        {
            this.Owner = owner;
            this.FileType = FileSystem.FileType.None;
            this.FullName = "";
            this.Name = "";
            this.Extension = "";
        }
        public override string ToString()
        {
            return this.FileType.ToString() + ": " + this.Name + "  (" + this.FullName + ")";
        }
        public FileGroup Owner { get; private set; }
        public FileType FileType { get; set; }
        public string FullName { get; set; }
        public string Name { get; set; }
        public string Extension { get; set; }
        public long Length { get; set; }
        public DateTime DateModify { get; set; }
        public string ToolTip { get; set; }
        public string IconKey { get; set; }
        public bool Selected { get; set; }
        public bool Filtered { get; set; }

        public static int CompareByName(FileItem a, FileItem b)
        {
            int cmp = ((int)a.FileType).CompareTo((int)b.FileType);
            if (cmp != 0) return cmp;
            return a.Name.CompareTo(b.Name);
        }

        internal void StoreColorsForItem(System.Windows.Forms.DataGridViewCellStyle cellStyle)
        {
            if (cellStyle == null) return;
            if (!this.Selected)
                cellStyle.BackColor = Color.DarkBlue;
            else
                cellStyle.BackColor = Color.DarkGreen;

            switch (this.FileType)
            {
                case FileSystem.FileType.UpDir:
                case FileSystem.FileType.DownDir:
                    cellStyle.ForeColor = Color.Yellow;
                    break;
                case FileSystem.FileType.File:
                    cellStyle.ForeColor = ColorForExt(this.Extension.ToLower());;
                    break;
            }
        }

        private Color ColorForExt(string ext)
        {
            switch (ext)
            {
                case "exe":
                case "com":
                    return Color.LightPink;
                case "dll":
                    return Color.LightSkyBlue;
                case "cs":
                    return Color.LightGreen;
            }
            return Color.Wheat;
        }
    }
    public enum FileType
    {
        None = 0,
        UpDir = 1,
        DownDir = 2,
        File = 3
    }
}
